<template>
  <div class="login-body">
    <div class="login-container">
      <div class="head">
        <img class="logo" src="/kyland.png"/>
      </div>
      <el-form label-position="top" :rules="state.rules" :model="state.ruleForm" ref="loginForm" class="login-form">
        <el-form-item :label="$t('username')" prop="username">
          <el-input type="text" v-model.trim="state.ruleForm.username" clearable>
            <template #prefix>
              <img class="loginIcon" src="/icon/user.png">
            </template>
          </el-input>
        </el-form-item>
        <el-form-item :label="$t('pwd')" prop="password">
          <el-input type="password" v-model="state.ruleForm.password" clearable autocomplete="new-password"
                    show-password @keyup.enter="submitForm">
            <template #prefix>
              <img class="loginIcon" src="/icon/password.png">
            </template>
          </el-input>
        </el-form-item>
        <el-form-item>
          <!--          <div style="color: #333">登录表示您已同意<a href="#">《服务条款》</a></div>-->
          <el-button style="width: 100%;margin-top: 15px" type="primary"
                     @click="submitForm">{{ $t('login') }}
          </el-button>
          <el-button style="width: 100%; margin-top: 15px; margin-left: 0;color: #C29B74" @click="resetForm">{{ $t('reset') }}
          </el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script setup>
import axios from '@/utils/axios'
import {reactive, ref, onMounted, inject} from 'vue'
import {useRouter} from 'vue-router'
import {useI18n} from 'vue-i18n'
import {localSet,encrypt} from '@/utils'
import {ElMessage} from "element-plus"
const router = useRouter()
const loginForm = ref(null)
const {t} = useI18n()
onMounted(() => {
  state.ruleForm.password = ''
})
const state = reactive({
  ruleForm: {
    username: '',
    password: ''
  },
  checked: true,
  rules: {
    username: [
      {required: 'true', message: t('userEmptyTip'), trigger: 'blur'}
    ],
    password: [
      {required: 'true', message: t('pwdEmptyTip'), trigger: 'blur'}
    ]
  }
})
const submitForm = async () => {
  loginForm.value.validate((valid) => {
    if (valid) {
      // 登陆判断
      const uname = state.ruleForm.username
      const auth = encrypt(state.ruleForm.password)
      axios.post('/login', {uname, auth}).
      then((data) => {
        console.log(data)

        if (data.login != -1) {
          localSet('token', {...data.info,uname})
          router.replace('/')
        } else {
          ElMessage.error('账号或密码错误!')
        }
      }).catch(() => {
        ElMessage.error('账号或密码错误!')
      })
    }
  })
}
const resetForm = () => {
  loginForm.value.resetFields();
}
</script>

<style scoped>
.loginIcon {
  width: 18px;
  height: 18px;
}

.logo {
  width: 200px;
  height: 70px;
}

.login-body {
  display: flex;
  position: relative;
  justify-content: center;
  align-items: center;
  width: 100%;
  height: 100%;
  background-image: url('/src/assets/icon/loginBackGround.jpg');
  background-size: cover;
  background-repeat: no-repeat;
}

.login-container {
  position: absolute;
  width: 480px;
  height: 580px;
  background-color: #fff;
  right: 360px;
  border-radius: 4px;
  box-shadow: 0px 21px 41px 0px rgba(0, 0, 0, 0.2);
}

.head {
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 40px 0 20px 0;
}

.head .title {
  font-size: 28px;
  color: #1BAEAE;
  font-weight: bold;
}

.head .tips {
  font-size: 12px;
  color: #999;
}

.login-form {
  width: 70%;
  margin: 0 auto;
}

.login-form >>> .el-form--label-top .el-form-item__label {
  padding: 0;
}

.login-form >>> .el-form-item {
  margin-bottom: 15px;
}
</style>